home *** CD-ROM | disk | FTP | other *** search
/ NetNews Offline 2 / NetNews Offline Volume 2.iso / news / comp / lang / c++-part2 / 11775 < prev    next >
Encoding:
Text File  |  1996-08-05  |  1.7 KB  |  72 lines

  1. Path: taco.cc.ncsu.edu!not-for-mail
  2. From: zaykin@espcbw.stat.ncsu.edu (Dmitri Zaykin)
  3. Newsgroups: comp.lang.c++
  4. Subject: Re: dynamic 3d array of  user defined variables(need help)
  5. Date: 16 Mar 1996 04:19:40 GMT
  6. Organization: Statistical Eugenics
  7. Distribution: world
  8. Message-ID: <4idfgs$t6m@taco.cc.ncsu.edu>
  9. References: <4iak05$3mg@cafu.fl.net.au>
  10. NNTP-Posting-Host: espcbw.stat.ncsu.edu
  11. X-Newsreader: TIN [UNIX 1.3 950824BETA PL0]
  12.  
  13. don@fl.net.au wrote:
  14. > I have been trying to write a function to create a  dynamic 3d array
  15. > of  user defined variables in Borland C++ 4.5 without much success.I
  16. > have managed to write a flexible function to define a 3d array but I
  17. > need to define one of co-ordinates and am having trouble with user
  18. > defined variables. I would appreciate any help you can offer.
  19.  
  20. this should work:
  21.  
  22. template <class T>
  23. T ***Trialloc(T *dummy, int fir, int sec, int thi)
  24. { // ignore 'param dummy never used' warning
  25.  T ***d;
  26.  int i,j;
  27.  d = new T**[ fir ];
  28.  for(i=0; i<fir; i++)
  29.  {
  30.    d[i] = new T*[ sec ];
  31.    for(j=0; j<sec; j++) {
  32.      d[i][j] = new T[ thi ];
  33.    }
  34.  }
  35.  return d;
  36. }
  37.  
  38. template<class T>
  39. void Trifree(T ***x, int fir, int sec)
  40. {
  41.   int i,j;
  42.   for(i=0; i < fir; i++) {
  43.    for(j=0; j < sec; j++) { delete [] x[i][j]; }
  44.   }
  45.   for(i=0; i < fir; i++) { delete [] x[i]; }
  46.   delete [] x;
  47. };
  48.  
  49.  
  50. // example
  51.  
  52. struct Data {
  53.   char buf[2];
  54.   Data *next;
  55.   Data() { buf[0]=buf[1]=0; next=0; }
  56. };
  57.  
  58. int main(void)
  59. {
  60.  Data *** x;
  61.  Data tmp;  // need 'tmp' to specify the data type when call 'Trialloc'
  62.  x = Trialloc(&tmp, 10, 20, 30);
  63.  // do something with x[i][j][k]
  64.  Trifree(x, 10, 20);
  65.  return 0;
  66. }
  67.  
  68. -- 
  69. Dima
  70. { the following appendum is a Cauchy random variable }
  71. The opera ain't over till the fat lady sings
  72.